
var initcatedatalink = webrootpath+"word/initcatedatalink";  //加载分类树
var addcatedatalink = webrootpath+"word/addcatedatalink";//添加词表分类内容
var deletecatedatabyidlink = webrootpath+"word/deletecatedatabyidlink"; //根据id删除词表分类数据

var i_w_addword_dialog_sublink = webrootpath+"word/i_w_addword_dialog_sublink"; //词表增加词内容
var getworddatalistlink = webrootpath+"word/getworddatalistlink";//获取分类下词表内容
var i_w_deletewordlink = webrootpath+"word/i_w_deletewordlink";//根据id删除某个词

var upload_file = webrootpath+"file/upload_file"; //上传文件
var delAttached_link = webrootpath+"doc/delAttached_link"; //删除附件
var i_w_importword_importlink = webrootpath+"word/i_w_importword_importlink";//批量导入附件

var i_w_searchwordlink = webrootpath+"word/i_w_searchwordlink";//检索词
var i_w_dumpwordlink = webrootpath+"word/i_w_dumpwordlink";//导出分类下词表
var indexwordorderby = "crdate desc";
var i_w_clearwordlink = webrootpath+"word/i_w_clearwordlink";//清空此分类下的词表内容

$(function () {
    findlmdata_link_fun(-1); //加载分类数据
    index_lm_tab(1);
});

/***
 * 清空当前词表
 */
function i_w_clearword() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var r = window.confirm("您确定清空此分类下的词表内容吗？");
    if(r){
        $.get(i_w_clearwordlink,{
            selectcateid:selectcateid
        },function (_info) {
            $.alert(_info.msg);
            getworddatalist(1,"crdate desc");
        });
    }
}

/**
 * 导出分类下所有的词表为excel
 */
function i_w_dumpword() {

    $("#init_loading").modal('show');

    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    $.post(i_w_dumpwordlink,{
        selectcateid:selectcateid
    },function (content) {
        $("#init_loading").modal('hide');
        if(content.code=="1"){
            $("#dump_path").html(content.data);
            $("#dump_path").attr("href",content.data);
            return false;
        }
    });
}

/***
 *  排序查询
 * @param _rderby
 */
function order_wordby(_this) {
    var orderbystr = $(_this).attr("orderbystr");
    var orderby = $(_this).attr("orderby");
    getworddatalist(1,orderbystr+"  "+orderby);
    indexwordorderby = orderbystr+"  "+orderby;
    if(orderby=="desc"){
        $(_this).attr("orderby","asc");
    }else {
        $(_this).attr("orderby","desc");
    }
}
/***
 * 检索词表
 */
function i_w_searchword() {
    var i_wsearchcontent = $.trim($("#i_wsearchcontent").val());
    if(i_wsearchcontent==""){
        $.alert("检索词不能为空");
        return false;
    }
    getworddatalist(1,"crdate desc");

}
/***
 * 批量导入词表_弹出框
 */
function i_w_importword() {
    $("#i_w_importword_dialog").modal("show");
    setTimeout(function () {
        $("#i_w_importword_picker").html("选择附件");
        loadworduploader();
    },500);
}

/***
 * 批量导入词表
 */
function i_w_importword_import() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateid =  nodes[0].cateid;
    var att_name_list = "";
    $("input[name='att_name']").each(function (_index,_this) {
        att_name_list+=","+_this.value;
    });
    if(att_name_list==""){
        return false;
    }

    $("#init_loading").modal("show");
    $.post(i_w_importword_importlink,{
        selectcateid:selectcateid,
        att_name_list:att_name_list
    },function (content) {
        $("#init_loading").modal("hide");
        if(content.code=="1"){
            $("#import_excelrule_dialog").modal('hide');
            $.alert({content:content.msg});
            getworddatalist(1,"crdate desc");
            return false;
        }
    });
}


/***
 * 加载上传组件
 */
function loadworduploader() {
    $("#i_w_importword_uploadlist").html("");
    $("#i_w_importword_list").html("");

    var uploader = WebUploader.create({
        swf: './webuploader-0.1.5/Uploader.swf',
        auto:true,
        server: upload_file,
        pick: '#i_w_importword_picker',
        resize: false,
        fileNumLimit:1,
        formData:{
            filetype:10
        },
        accept:{
            title: '文本内容',
            extensions: 'xlsx',
            mimeTypes: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        },
        fileSizeLimit:10485760000
    });
    uploader.on( 'fileQueued', function( file ) {

        $("#i_w_importword_uploadlist").html( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>' );
    });
    uploader.on( 'beforeFileQueued', function( file ) {
        uploader.reset();
    });

    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
                '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                '</div>' +
                '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中并正在处理，请稍等！！！');
        $percent.css( 'width', percentage * 100 + '%' );
    });
    //上传成功
    uploader.on( 'uploadSuccess', function( file,response ) {
        $( '#'+file.id ).find('p.state').text('上传完毕');

        setTimeout(function(){
            if(response.code=='1'){
                var htmlstr = "<tr id=\"att"+response.data.attachedid+"\" >"+
                    "<td>"+response.data.truename+"<input name='att_name' value='"+response.data.attachedid+"' style='display: none;' /></td>"+
                    "<td>"+response.data.size+"</td>"+
                    "<td><span class=\"label label-danger\" style='cursor: pointer;'  onclick=\"delAttached('"+response.data.attachedid+"')\">删除</span></td>"+
                    "</tr>";

                $("#i_w_importword_list").append(htmlstr);
            }
            $("#i_w_importword_uploadlist").html("");
            uploader.reset();
        },1000);

    });
    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
        uploader.reset();
    });
    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });
}



/***
 * 删除词
 * @param _objid
 * @param _wordname
 */
function i_w_deleteword(_objid,_wordname) {
    var r = window.confirm("您确定删除["+_wordname+"]吗？");
    if(r){
        $.get(i_w_deletewordlink,{
            objid:_objid
        },function (_info) {
            $.alert(_info.msg);
            getworddatalist(1,"crdate desc");
        });
    }
}

/***
 * 修改词弹出框
 * @param _objid
 * @param _wordname
 * @param _wordnamesy
 * @param worddesc
 * @param objstatus
 */
function i_w_updateword(_objid,_wordname,_wordnamesy,_worddesc,_objstatus) {
    $("#i_w_addword_dialog_objid").val(_objid);
    $("#i_w_addword_dialog_wordname").val(_wordname);
    $("#i_w_addword_dialog_wordname_sy").val(_wordnamesy);
    $("#i_w_addword_dialog_worddesc").val(_worddesc);
    $("#i_w_addword_dialog_wordstatus").val(_objstatus);

    $("#i_w_addword_dialog").modal('show');

    $("#i_w_addword_dialog_update").show();
    $("#i_w_addword_dialog_add").hide();
}

/***
 * 获取选中分类下词表内容
 * @param _pagenum
 */
function getworddatalist(_pagenum,_orderby) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateidid = nodes[0].cateid;

    var i_wsearchcontent = $.trim($("#i_wsearchcontent").val());


    $.post(getworddatalistlink,{
        i_wsearchcontent:i_wsearchcontent,
        selectcateidid:selectcateidid,
        pagenum:_pagenum,
        orderby:_orderby
    },function (_info) {
        $("#index_lm_doc_content").html("暂无数据");
        if(_info.code=="1"){
            var infodata = _info.data;
            var htmlstr = "";
            for(var i in infodata){
                var dataobj = infodata[i];

                var wordstatus = "";
                if(dataobj.objstatus==10){
                    wordstatus = "正常";
                }else if(dataobj.objstatus==20){
                    wordstatus = "停用";
                }

                htmlstr += "<tr>" +
                                "<td>"+i+"</td>" +
                                "<td style='overflow: hidden;white-space:nowrap;text-overflow: ellipsis;max-width:100px;' title='"+dataobj.wordname+"' >"+dataobj.wordname+"</td>" +
                                "<td style='overflow: hidden;white-space:nowrap;text-overflow: ellipsis;max-width:100px;' title='"+dataobj.wordnameSy+"'>"+dataobj.wordnameSy+"</td>" +
                                "<td style='overflow: hidden;white-space:nowrap;text-overflow: ellipsis;max-width:200px;' title='"+dataobj.worddesc+"'>"+dataobj.worddesc+"</td>" +
                                "<td>"+wordstatus+"</td>" +
                                "<td>"+timestampToTime(dataobj.crdate)+"</td>" +
                                "<td>" +
                                    "<button type='button' class='btn btn-danger'  onclick='i_w_deleteword(\""+dataobj.objid+"\",\""+dataobj.wordname+"\")'>删除</button>" +
                                    "<button type='button' class='btn btn-info'    onclick='i_w_updateword(\""+dataobj.objid+"\",\""+dataobj.wordname+"\",\""+dataobj.wordnameSy+"\",\""+dataobj.worddesc+"\",\""+dataobj.objstatus+"\")' style='margin-left: 5px;'>修改</button>" +
                                "</td>" +
                          "</tr>";

            }
            getworddatalist_pages(_pagenum,_info.pages);
            $("#index_lm_doc_content").html(htmlstr);
        }else {
            getworddatalist_pages(_pagenum,1);
            $("#index_lm_doc_content").html("暂无数据。");
        }
    });
}

/***
 * 获取分类下词表内容_分页
 */
function getworddatalist_pages(_pagenum,_pages){
    //处理分页
    $('#index_lm_cont_1_page').bootstrapPaginator({
        currentPage: _pagenum,
        totalPages: _pages,
        size:"normal",
        bootstrapMajorVersion: 3,
        alignment:"right",
        numberOfPages:5,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "末页";
                case "page":
                    return page;
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
            getworddatalist(page,indexwordorderby);
        }
    });
}


/****
 * 添加词表弹出框
 */
function i_w_addword() {
    $("#i_w_addword_dialog_objid").val("");
    $("#i_w_addword_dialog_wordname").val("");
    $("#i_w_addword_dialog_wordname_sy").val("");
    $("#i_w_addword_dialog_worddesc").val("");
    $("#i_w_addword_dialog").modal('show');
    $("#i_w_addword_dialog_add").show();
    $("#i_w_addword_dialog_update").hide();
}
/***
 * 添加，修改词表弹出框—提交内容
 */
function i_w_addword_dialog_sub(_type) {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var selectcateidid = nodes[0].cateid;
    var i_w_addword_dialog_objid = $("#i_w_addword_dialog_objid").val();
    var i_w_addword_dialog_wordname = $.trim($("#i_w_addword_dialog_wordname").val());
    var i_w_addword_dialog_wordname_sy =$.trim($("#i_w_addword_dialog_wordname_sy").val());
    var i_w_addword_dialog_worddesc =$.trim($("#i_w_addword_dialog_worddesc").val());
    var i_w_addword_dialog_wordstatus =$.trim($("#i_w_addword_dialog_wordstatus").val());
    if(i_w_addword_dialog_wordname=="" || i_w_addword_dialog_wordname_sy==""){
        $.alert("正名和异名为必填项");
        return false;
    }
    $.post(i_w_addword_dialog_sublink,{
        selectcateidid:selectcateidid,
        i_w_addword_dialog_wordname:i_w_addword_dialog_wordname,
        i_w_addword_dialog_wordname_sy:i_w_addword_dialog_wordname_sy,
        i_w_addword_dialog_worddesc:i_w_addword_dialog_worddesc,
        i_w_addword_dialog_wordstatus:i_w_addword_dialog_wordstatus,
        i_w_addword_dialog_objid:i_w_addword_dialog_objid,
        type:_type
    },function (_info) {
        $("#i_w_addword_dialog").modal('hide');
        if(_info.code=="1"){
            $.alert(_info.msg);
            getworddatalist(1,"crdate desc");
        }
    });
}
/***
 * 分类导航
 * @param _index
 */
function index_lm_tab(_index) {
    if (_index=="1"){
        $("#index_lm_ul li:eq(0)").addClass("active");
        $("#index_lm_cont_1").show();
    }
}
/***
 * 获取词表分类数据
 */
function findlmdata_link_fun(_node) {
    $.get(initcatedatalink,{node:_node},function (data) {
        if (data.code=='1'){
            initTree(data.data);
        }
    });
}


/***
 * 添加节点弹出窗口
 */
function index_lm_youjian_addnode() {
    index_lm_youjian_unb();
    $("#model_lm_nodename").val("");
    $("#model_lm_nodedesc").val("");
    $("#model_lm").modal('show');
}
/***
 * 添加节点提交更改
 */
function model_lm_model_submit() {
    var model_lm_nodename = $.trim($("#model_lm_nodename").val());
    var model_lm_nodedesc = $.trim($("#model_lm_nodedesc").val());
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    $.post(addcatedatalink,{node_name:model_lm_nodename,node_desc:model_lm_nodedesc,selectlmid:nodes[0].cateid},function (data) {
        if (data.code=="1"){
            $("#model_lm").modal('hide');

            $.get(initcatedatalink,{node:data.data},function (data) {
                if (data.code=='1'){
                    var selecttreeobj = $.fn.zTree.getZTreeObj("index_lm_tree");
                    var nodes = selecttreeobj.getSelectedNodes();
                    selecttreeobj.removeChildNodes(nodes[0]);
                    selecttreeobj.addNodes(nodes[0], data.data.datalist);
                }
            });

        }
    });
}
/***
 * 删除分类节点
 */
function index_lm_youjian_deletenode() {
    var treeObj = $.fn.zTree.getZTreeObj("index_lm_tree");
    var nodes = treeObj.getSelectedNodes();
    var r=window.confirm("您确定删除此分类吗？");
    if (r==true){
        $.get(deletecatedatabyidlink,{lmid:nodes[0].cateid},function (msg) {
            if(msg.code=='1'){
                $.alert("删除成功");
                treeObj.removeNode(nodes[0]);
            }
        });
    }
}
/***
 * 取消右键菜单
 */
function index_lm_youjian_unb() {
    $("#index_lm_youjian").css("display","none");
}
function index_lm_youjian_over() {
    $("#index_lm_youjian").css("display","block");
}
/***
 * 初始化树结构
 */
function initTree(_treedata) {
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data:{
            keep:{
                parent:true
            }
        },
        callback:{
            onRightClick:function (event, treeId, treeNode) {
                zTreeObj.selectNode(treeNode);
                var clientx = event.clientX-210;
                var clienty = event.clientY-50;

                $("#index_lm_youjian_cateid").html(treeNode.cateid);
                $("#index_lm_youjian").css({"margin-left":clientx,"margin-top":clienty,"display":"block"});

            },
            onExpand:function (event, treeId, treeNode) {

                var zAsync = treeNode.zAsync;
                //展开
                var treenodeid = treeNode.cateid;
                $.get(initcatedatalink,{node:treenodeid},function (data) {
                    if (data.code=='1'){
                        var sonnode = data.data.datalist;
                        zTreeObj.addNodes(treeNode, sonnode);
                    }
                });
            },
            onCollapse:function (event, treeId, treeNode) {
                //折叠后
                var treenodeid = treeNode.cateid;
                zTreeObj.removeChildNodes(treeNode);
            },
            onClick:function (event, treeId, treeNode,clickFlag) {
                //点击选择
                $("#i_wsearchcontent").val("");//清空检索框
                index_lm_tab(1);
                getworddatalist(1,"crdate desc");
            }
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    zTreeObj = $.fn.zTree.init($("#index_lm_tree"), setting, _treedata.datalist);
    var nodes = zTreeObj.getNodes();
    zTreeObj.selectNode(nodes[0]);
    getworddatalist(1,"crdate desc");
    index_lm_tab(1);
}

/***
 *  删除附件
 */
function delAttached(attachedid) {
    $.confirm({
        title:"提示",
        content:"您确认删除此附件吗？",
        buttons:{
            "确定":function () {
                $.get(delAttached_link,{attachedid:attachedid},function (msg) {
                    if (msg.code=='1'){
                        $("#att"+attachedid).remove();
                        $.confirm({
                            title: '提示',
                            content: '成功删除附件！',
                            autoClose: '确定|2000',
                            buttons: {
                                确定: {
                                }
                            }
                        });
                    }
                });
            },
            "取消":function () {

            }
        }
    });
}